<template>
  <el-dialog
    :before-close="handleClose"
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
      <el-form-item label="选择教师" prop="userId">
          <el-select v-model="dataForm.userId" filterable placeholder="请选择">
            <el-option
              v-for="item in selectList"
              :key="item.userId"
              :label="item.username"
              :value="item.userId">
            </el-option>
          </el-select>
      </el-form-item>
      <el-form-item label="选择班级" prop="classId">
          <el-select v-model="dataForm.classId" :disabled="disabled" filterable placeholder="请选择">
            <el-option
              v-for="item in classList"
              :key="item.classId"
              :label="item.className"
              :value="item.classId">
            </el-option>
          </el-select>
      </el-form-item>
      <el-form-item label="角色" size="mini" prop="roleIdList">
        <el-checkbox-group v-model="dataForm.roleIdList">
          <el-checkbox v-for="role in roleList" :key="role.roleId" :label="role.roleId">{{ role.roleName }}</el-checkbox>
        </el-checkbox-group>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        disabled: false,
        roleList: [],
        dataForm: {
          id: 0,
          userName: '',
          roleIdList: [],
          status: 1,
          classId: '',
          userId: ''
        },
        classList: [],
        selectList: [],
        dataRule: {
          id: [
            { required: true, message: '教师不能为空', trigger: 'change' }
          ],
          classId: [
            { required: true, message: '所属班级不能为空', trigger: 'change' }
          ],
          roleIdList: [
            { required: true, message: '角色不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.$http({
          url: this.$http.adornUrl('/secretary/headmaster/select'),
          method: 'get',
          params: this.$http.adornParams()
        }).then(({data}) => {
          this.roleList = data && data.code === 0 ? data.list : []
        }).then(() => {
            this.$http({
              url: this.$http.adornUrl(`/generator/class/selectAll`),
              method: 'get',
              params: this.$http.adornParams()
        }).then(({data}) => {
              if(data.code == 0){
                this.classList = data.class;
              }
        })
        })
        .then(() => {
            this.$http({
              url: this.$http.adornUrl(`/secretary/teacher/selectTeachers`),
              method: 'get',
              params: this.$http.adornParams()
        }).then(({data}) => {
              if(data.code == 0){
              this.selectList = data.teachers
              console.log('this.selectList',this.selectList)
              console.log('teachers',data.teachers)
              this.visible = true
              this.$nextTick(() => {
              this.$refs['dataForm'].resetFields()
          })
          }
        })
        }).then(() => {
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/sys/user/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.disabled = true
                this.dataForm.userId = data.user.userId
                this.dataForm.classId = data.user.classId
                this.dataForm.roleIdList = data.user.roleIdList
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/secretary/headmaster/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'userId': this.dataForm.userId || undefined,
                'roleIdList': this.dataForm.roleIdList,
                'classId': this.dataForm.classId
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.disabled = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      },
      handleClose(done) {
        done();
        this.disabled = false;
      }
    }
  }
</script>
